# -*- coding: utf-8 -*-
'''
Created on Apr 3, 2012

@author: LONG HOANG GIANG
'''

import os, sys
sys.path.append(os.path.join(os.path.dirname(__file__), '../'))
import simplejson as json
import gzip
import datetime
from CrawlerLib.http import Http

FILE_DATA = '/home5/vietcntt/public_html/site-api-vietcntt/res/truyen/detective-conan/output.gz'

def loadData(fileName):
    f = gzip.open(fileName, 'rb')
    content = f.read()
    f.close()
    data = json.loads(content)
    return data;

def saveData(fileName, data):
    content = json.dumps(data)
    f = gzip.open(fileName, 'wb')
    f.write(content)
    f.close()

def pushFirst(item):
    data = loadData(FILE_DATA)
    n = len(data)
    data.append(None)
    for i in range(n, 0, -1):
        data[i] = data[i-1]
    data[0] = item;
    saveData(FILE_DATA, data)

def pushLast(item):
    data = loadData(FILE_DATA)
    data.append(item)
    saveData(FILE_DATA, data)

def getImagesOfChapter(url, chapterName):
    
    tree = Http.getXMLTree(url)
    xpathNode = "//div[@class='entry']//img"
    for item in tree.xpath(xpathNode):
        print item.get('src')
    
def update(chapterName):
    
    images = '''http://4.bp.blogspot.com/-AdhSgooGGQI/T5tN-Kz6UYI/AAAAAAABxC0/XR_Ww44_8nY/s1200/00.jpg
http://4.bp.blogspot.com/-KnMjEvn6qeY/T5tN8cMghGI/AAAAAAABxCo/3KJvkYNeqL0/s1200/01.jpg
http://4.bp.blogspot.com/-eZS_t0ANxtA/T5tN7vyAb-I/AAAAAAABxCk/NIJK18Ifxz8/s1200/02.jpg
http://3.bp.blogspot.com/-agxeyANmimo/T5tN_vzVdII/AAAAAAABxC8/JbIYbMdk3zc/s1200/03.jpg
http://3.bp.blogspot.com/-IL5FWu-ZG0s/T5tN_0T0t7I/AAAAAAABxDA/7KcFtq4D8do/s1200/04.jpg
http://4.bp.blogspot.com/-ZdHDck1Whtc/T5tOB3bP8FI/AAAAAAABxDM/UGsj1zJFFz0/s1200/05.jpg
http://4.bp.blogspot.com/-I_xYVU0aHrg/T5tOEzPhPbI/AAAAAAABxDY/MDen_pBH7FM/s1200/06.jpg
http://4.bp.blogspot.com/-wRms1Kk7YSg/T5tOEdKD2HI/AAAAAAABxDU/AyIuGLVE_Sg/s1200/07.jpg
http://4.bp.blogspot.com/-859At0Voy9U/T5tOGIzOnoI/AAAAAAABxDk/JVXlGIO4kH0/s1200/08.jpg
http://4.bp.blogspot.com/-PyBJLneIzM0/T5tOJCcEGaI/AAAAAAABxDs/UMfhBJCljz8/s1200/09.jpg
http://3.bp.blogspot.com/-c-dTq3oI9Sg/T5tOJfZ-MuI/AAAAAAABxDw/6p776_qJWso/s1200/10.jpg
http://4.bp.blogspot.com/-8QaCQUGruuc/T5tOLGDuK_I/AAAAAAABxD8/GuaFewIpMzo/s1200/11.jpg
http://3.bp.blogspot.com/-CPrjTZTXx7M/T5tOOn79ckI/AAAAAAABxEM/7Q0D0EazS3k/s1200/12.jpg
http://4.bp.blogspot.com/-flWbh1lOl20/T5tOOhnAUNI/AAAAAAABxEE/vYJCruIKlhM/s1200/13.jpg
http://4.bp.blogspot.com/-KtGXnVEpp8g/T5tOPC5FRoI/AAAAAAABxEQ/oIadmUjImkk/s1200/14.jpg
http://3.bp.blogspot.com/-vZC_uYRPZLQ/T5tORyIYNHI/AAAAAAABxEg/wyVPBIM7Ses/s1200/15.jpg
http://3.bp.blogspot.com/-ulipP16rEPo/T5tOUn8tT_I/AAAAAAABxEs/LpW_RTqSvEM/s1200/16.jpg
http://4.bp.blogspot.com/-8ebsQrRnqK8/T5tOUBvxxfI/AAAAAAABxEo/-LMU3lUNEeI/s1200/17.jpg'''
    
    images = images.strip().split("\n")
    item = {'chapter': chapterName, 'images': images}
    pushFirst(item)
        
if __name__ == '__main__':
    
#    getImagesOfChapter('http://doctruyen.vechai.info/conan-chap-815/', 'Detective Conan 815')
    update('Detective Conan 815')
    
    print('>>Finished at {0}'.format(datetime.datetime.now()))
    os._exit(1)

    